package com.example.treehole.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.treehole.bean.Comment;
import com.example.treehole.bean.Post;
import com.example.treehole.bean.User;
import com.example.treehole.common.BaseResult;
import com.example.treehole.service.CommentServiceImpl;
import com.example.treehole.service.PostServiceImpl;
import com.example.treehole.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/post")
public class PostController {
    @Autowired
    private PostServiceImpl postService;


    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private CommentServiceImpl commentService;


    @PostMapping("sendComment")
    public ResponseEntity<?> sendComment(@RequestBody Comment comment) {
        commentService.save(comment);
        return BaseResult.ok("发送成功");
    }

    @PostMapping("sendPost")
    public ResponseEntity<?> sendPost(@RequestBody Post post) {
        postService.save(post);
        return BaseResult.ok("发布成功");
    }

    @PostMapping("updatePost")
    public ResponseEntity<?> updatePost(@RequestBody Post post) {
        postService.updateById(post);
        return BaseResult.ok("修改成功");
    }

    @GetMapping("getCommentById")
    public ResponseEntity<?> getCommentById(String postId) {
        List<Comment> list = commentService.list(new LambdaQueryWrapper<Comment>().eq(Comment::getPostId, postId));
        for (Comment comment : list) {
            if (comment.anonymous) {
                comment.nickname = "匿名用户";
                comment.face = "http://d.hiphotos.bdimg.com/wisegame/pic/item/320fd9f9d72a605946395ca92b34349b033bba5c.jpg";
            } else {
                User user = userService.getById(comment.userid);
                comment.nickname = user.nickname;
                comment.face = user.face;
            }

            if (comment.toId != null) {
                comment.toUser = userService.getById(comment.toId);
            }
        }
        return BaseResult.ok("获取成功", list);
    }


    @GetMapping("postSearch")
    public ResponseEntity<?> postSearch(String key, String type, String hot) {

        List<Post> postList = postService.list(new LambdaQueryWrapper<Post>()
                .eq(!type.equals("全部"), Post::getType, type)
                .like(key != null, Post::getTitle, key)
                .orderByDesc(Post::getTime));
        if (!hot.equals("时间")) {
            postList.sort((o1, o2) -> {
                int c1 = commentService.lambdaQuery().eq(Comment::getPostId, o1.id).count();
                int c2 = commentService.lambdaQuery().eq(Comment::getPostId, o2.id).count();
                return c2 - c1;
            });
        }
        return getUserInfo(postList);

    }

    private ResponseEntity<?> getUserInfo(List<Post> postList) {
        for (Post post : postList) {
            if (!post.anonymous) {
                User user = userService.getById(post.userid);
                post.nickname = user.nickname;
                post.face = user.face;
            } else {
                post.nickname = "匿名用户";
                post.face = "http://d.hiphotos.bdimg.com/wisegame/pic/item/320fd9f9d72a605946395ca92b34349b033bba5c.jpg";
            }

            System.out.println(post);
        }
        return BaseResult.ok("获取成功", postList);
    }

    @GetMapping("getMyPost")
    public ResponseEntity<?> getMyPost(String userid) {
        List<Post> postList = postService.list(new LambdaQueryWrapper<Post>().eq(Post::getUserid, userid).orderByDesc(Post::getTime));
        getUserInfo(postList);
        return BaseResult.ok("获取成功", postList);
    }

    @DeleteMapping("deletePost")
    public ResponseEntity<?> deletePost(String id) {
        postService.removeById(id);
        return BaseResult.ok("删除成功");
    }

    @DeleteMapping("deleteComment")
    public ResponseEntity<?> deleteComment(String id) {
        commentService.removeById(id);
        return BaseResult.ok("删除成功");
    }


}
